package joyi.train.cheng.qmd;

import j.framework.core.LoginUser;
import j.framework.qmd.core.QueryDataHandler;
import j.framework.qmd.core.QueryFieldMetadata;
import j.framework.qmd.core.QueryMetadata;
import j.framework.qmd.core.QueryResult;
import j.util.StringUtils;
import j.util.jdbc.QueryJdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * Created by WY2015092804 on 2015/10/18.
 */
public class CustormerQueryHandler implements QueryDataHandler {
    @Override
    public Object getColumnValue(QueryFieldMetadata fieldMetadata, Object value, Map<String, Object> rowData) {
        if ("gender".equals(fieldMetadata.getFieldName())) {
            if ("F".equals(value) || "f".equals(value)) {
                return "男";
            } else if ("m".equals(value) || "M".equals(value)) {
                return "女";
            } else {
                return "未知";
            }
        }
        return value;
    }

    @Override
    public String buildWhereClause(LoginUser user, Map<String, String> queryParams, List<Object> args) {
        String cityId = queryParams.get("cityId");
        StringBuilder sql = new StringBuilder("1=1");
        if (StringUtils.isNotEmpty(cityId)){
            sql.append(" and cityId =?");
            args.add(cityId);
        }
        return sql.toString();
    }

    @Override
    public void prepareCall(LoginUser user, Map<String, String> queryParams, Map<String, Object> callParams) {

    }

    @Override
    public String getOperatorHtml(LoginUser user, Map<String, Object> rowData) {
        StringBuilder opt = new StringBuilder();
        opt.append("<a href = 'javascript:void(0);' onClick =\"editCustormer('" + rowData.get("custormerId") + "')\">编辑</a>");
        opt.append(" <a href = 'javascript:void(0);' onClick =\"delCustormer('" + rowData.get("custormerId") + "')\">删除</a>");
        return opt.toString();
    }

    @Override
    public boolean beforeQuery(QueryJdbcTemplate qjt, QueryMetadata qmd, Map<String, String> queryParams, QueryResult queryResult) {
        return true;
    }

    @Override
    public void doAfterQuery(QueryJdbcTemplate qjt, QueryMetadata qmd, Map<String, String> queryParams, QueryResult queryResult) {

    }
}
